<template>
	<view class="">
		<!-- 顶部导航 -->
		<!-- <fa-navbar :safeAreaInsetTop="false" :title="title" ref="navbar"></fa-navbar> -->
		<view class="content" :style="{'background':'url('+blockDetail.image+') no-repeat','background-size':'cover'}">
			<view class="login">
				<view class="login-mobile" v-if="checkLogintype('mobile')">
					<view class="u-m-t-50">
						<u-form :model="form" :rules="rules" ref="uForm" :errorType="errorType">
							<u-form-item :label-position="labelPosition" label-width="140">
								<u-input placeholder="请输入手机号码" v-model="form.mobile" type="number"
									shape="circle" :customStyle="{'backgroundColor':'#ffffff'}">
									<u-text text="+86" slot="prefix" size="30" margin="0 3px 0 0" type="tips"></u-text>
								</u-input>
								
							</u-form-item>
							<u-form-item label-width="140">
								<text class="tips">未注册的手机号验证后自动创建账户</text>
							</u-form-item>
							
							<u-form-item label-width="140">
								<u-button type="primary"
									:customStyle="{'backgroundColor':'#ffffff','color':theme.bgColor}" shape="circle"
									text="获取短信验证码" @click="sendCode"></u-button>
							</u-form-item>

							<u-form-item>
								<view class="pwd-login">
									<!-- <text>密码登录</text> -->
								</view>
							</u-form-item>
						</u-form>
					</view>
				</view>

				<!-- #ifdef MP-WEIXIN -->
				<view class="agree">
					<view class="u-checkbox">
						<u-checkbox-group @change="checkboxChange">
							<u-checkbox v-model="agreeChecked" activeColor="#FB9F1B" shape="circle" size="26"
								labelSize="26" labelColor="#333333" label="我已同意并阅读">
								
							</u-checkbox>
							<text class="yhxy">《用户协议》</text>
						</u-checkbox-group>
					</view>
				</view>
				<view class="u-text-center u-m-t-40" v-if="checkLogintype('wechatmobile')">
					<u-button hover-class="none" open-type="getPhoneNumber" type="primary"
						:custom-style="{ backgroundColor: theme.bgColor, color: '#fff'}" shape="circle"
						@getphonenumber="getPhoneNumber">
						手机号快捷登录
					</u-button>
				</view>
				<!-- #endif -->
				
				
			</view>
		</view>
		<u-toast ref="uToast" /> 
	</view>
</template>

<script>
	import {
		loginfunc
	} from '@/common/fa.mixin.js';
	export default {
		mixins: [loginfunc],
		computed: {
			
			blockDetail(){
				return this.vuex_config.blockList.loginbgimg || [];
			}
		
		},
		onLoad() {
			// #ifdef MP-WEIXIN || APP-PLUS
			this.isThreeLogin = true;
			// #endif

			// #ifdef H5
			if (this.$util.isWeiXinBrowser()) {
				this.isThreeLogin = true;
			}
			// #endif

			// 如果仅有账号登录一种方式则直接跳转至登录页
			if (this.checkLogintype('account') && this.vuex_config.logintypearr.length === 1) {
				uni.$u.route({
					url: '/pages/login/login',
					type: 'redirectTo'
				});
			}
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			if (this.checkLogintype('mobile')) {
				this.$refs.uForm.setRules(this.rules);
			}
		},
		data() {
			return {
				labelPosition: 'top',
				border: false,
				errorType: 'message',
				form: {
					mobile: '',
					captcha: '',
					agree: []
				},
				rules: {
					mobile: [{
							required: true,
							message: '请输入手机号码',
							// 可以单个或者同时写两个触发验证方式
							trigger: ['change', 'blur']
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								return this.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							trigger: ['change', 'blur']
						}
					],
					captcha: [{
						required: true,
						message: '请输入验证码',
						trigger: 'change'
					}]
				},
				isThreeLogin: false,
				codeTips: '',
				canGetCode: true,
				title:'',
				agreeChecked: false,
			};
		},
		methods: {
			//获取验证码
			sendCode: async function() {
				if (!uni.$u.test.mobile(this.form.mobile)) {
					uni.$u.toast('手机号码格式不正确！');
					return;
				}
				if (this.form.agree.length == 0) {
					uni.$u.toast('请阅读用户协议！');
					return;
				}

				if (this.canGetCode) {
					let res = await this.$api.getSmsSend({
						mobile: this.form.mobile,
						event: 'mobilelogin'
					});
					setTimeout(() => {
						uni.$u.toast(res.msg);
					}, 50);
					if (res.code == 1) {
						this.canGetCode = false;
						uni.$u.route({
							url: '/pages/login/registercode',
							type: 'redirect',
							params: {
								mobile: this.form.mobile
							}
						})
					}
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}

			},
			checkboxChange(e) {
				this.agreeChecked = e
			},
			codeChange(text) {
				this.codeTips = text;
			},
			// 获取验证码
			getCode: async function() {
				if (!this.$u.test.mobile(this.form.mobile)) {
					this.$u.toast('手机号码格式不正确！');
					return;
				}
				if (this.$refs.uCode.canGetCode) {
					let res = await this.$api.getSmsSend({
						mobile: this.form.mobile,
						event: 'mobilelogin'
					});
					setTimeout(() => {
						this.$u.toast(res.msg);
					}, 50);
					if (res.code == 1) {
						this.$refs.uCode.start();
					}
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			goThreeLogin: async function() {
				// #ifdef MP-WEIXIN
				this.goMpLogin();
				// #endif

				// #ifdef H5
				this.goAuth();
				// #endif

				// #ifdef APP-PLUS
				this.goAppLogin();
				// #endif
			},
			goLogin: function() {
				this.$refs.uForm.validate(async valid => {
					if (valid) {
						if (this.vuex_wx_uid) {
							this.form.wx_user_id = this.vuex_wx_uid;
						}
						let res = await this.$api.mobilelogin(this.form);
						if (!res.code) {
							this.$u.toast(res.msg);
							return;
						}
						this.$u.vuex('vuex_token', res.data.token);
						this.success();
					} else {
						this.$u.toast('验证失败');
					}
				});
			}
		}
	};
</script>

<style lang="scss" scoped>

	.content {
		// margin-top: 178rpx;
		padding: 24rpx;
		height: 100vh;
	}
	.box{
		background-color: #ffffff;
		padding: 24rpx 24rpx;
		border-radius: 20rpx;
	}
	.content .logo {
		text {
			display: block;
			color: #333333;
			line-height: 70rpx;
		}

		.font-s {
			font-family: Georgia, 'Times New Roman', Times, serif;
			font-size: 60rpx;
			font-weight: 600;
		}

		.slogan {
			font-size: 26rpx;
			font-weight: 600;
		}
	}

	.login {
		position: absolute;
		bottom: 100rpx;
		left: 24rpx;
		right: 24rpx;
		background-color: #ffffff;
		padding: 24rpx;
		border-radius: 20rpx;
		.tips {
			font-size: 26rpx;
			font-weight: 600;
			color: #ffffff;
			padding-top: 30rpx;
			text-align: center;
		}

		.login-mobile {
			margin-top: 100rpx;

			.u-input {
				padding: 20rpx !important;
				padding-left: 60rpx !important;
			}

			.u-button {
				height: 88rpx;
				font-weight: 600;
				font-size: 26rpx;
			}

			.pwd-login {
				text-align: center;
				color: #ffffff;
				font-weight: 600;
			}
		}
	}

	.agree {
		.u-checkbox {
			.u-checkbox-group {
				text {
					font-size: 26rpx !important;
					font-family: 600;
				}
			}

			margin-left: 60rpx;

			.yhxy {
				color: #333333;
			}
		}
	}
	.u-form-item__body__right__content__slot{
		display: block !important;
	}
</style>